package com.chixing.day20.day20_Home;

import java.util.*;
import java.util.stream.Collectors;

public class PersonTest {
    public static void main(String[] args) {
        Person per = new Person("张小红",18);
        Person per1 = new Person("王小明",19);
        Person per2= new Person("刘晓刚",21);
        Person per3 = new Person("晓丽",23);
        Person per4 = new Person("王刚",25);
        Person per5 = new Person("王萌萌",25);

        List<Person> persons = new ArrayList<Person>();
        persons.add(per);
        persons.add(per1);
        persons.add(per2);
        persons.add(per3);
        persons.add(per4);
        persons.add(per5);

        //1
        List<Person> people = persons.stream().filter((f) -> f.getPerName().length() ==3).collect(Collectors.toList());
        System.out.println(people);

        //2
        List<Person> people2 = persons.stream().limit(3).collect(Collectors.toList());
        System.out.println(people2);

        //3
        List<Person> people3 = persons.stream().filter((d) -> d.getPerName().charAt(0) == '张').collect(Collectors.toList());
        System.out.println(people3);

        //4
        List<Person> people4 = persons.stream().skip(2).collect(Collectors.toList());
        System.out.println(people4);

        //5
        Map<Character, Integer> map = new HashMap<>();
        for (Person person : persons) {
            char firstName = person.getPerName().charAt(0);
            if(map.containsKey(firstName)) {
                map.put(firstName, map.get(firstName) + 1);
            }else{
                map.put(firstName,1);
            }
        }
        Set<Character> set = map.keySet();
        for (Character c : set ) {
            List<Person> people5 = persons.stream().filter((n) -> n.getPerName().charAt(0) == c).collect(Collectors.toList());
            System.out.println(people5);
        }
    }
}
